package com.veng.springbootdemo.controller;

import com.veng.springbootdemo.service.UserService;
import com.veng.springbootdemo.service.UserServiceImp;
import com.veng.springbootdemo.utils.ResponseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@RestController
public class HelloController {

    @Autowired
    private UserService userService ;


    @RequestMapping(value = "modifyPassword",method = RequestMethod.POST)
    public Map modifyPassword(@RequestParam("username")String username,@RequestParam("password")String password){
        userService.modifyPasswordByUsername(username,password);
        return ResponseUtils.toJson(200, "修改成功", null);
    }

    @RequestMapping(value = "user",method = RequestMethod.POST)
    public boolean hello(HttpSession session)  {
//        User user = userService.getUser();
        return (boolean) session.getAttribute("login");
    }
    @RequestMapping(value = "all",method = RequestMethod.GET)
    public Map all(@RequestParam("page")Integer page,@RequestParam("size")Integer size){
        List<com.veng.springbootdemo.po.User> all = userService.findAll(page,size);
        return ResponseUtils.toJson(200, "用户列表", all);
    }

    @RequestMapping(value = "first",method = RequestMethod.GET)
    public Map first(){
        com.veng.springbootdemo.po.User firstByUsername = userService.findFirstByUsername();
        return ResponseUtils.toJson(200, "first", firstByUsername);
    }
    /**
     * 注册
     * @param username 用户名
     * @param password 密码
     * @return
     */
    @RequestMapping(value = "register",method = RequestMethod.POST)
    public Map register(@RequestParam("username") String username,
                        @RequestParam("password") String password,
                        @RequestParam("email")String email){
        try {
            userService.register(username,password,email);
        } catch (UserServiceImp.UsernameEmptyException e) {//用户名为空
            return ResponseUtils.toJson(0, "用户名为空", null);
        } catch (UserServiceImp.PasswordEmptyException e) {//密码为空
            return ResponseUtils.toJson(1, "密码为空", null);
        } catch (UserServiceImp.EmailEmptyException e) {//邮箱为空
            return ResponseUtils.toJson(2, "邮箱为空", null);
        } catch (UserServiceImp.EmailInvalidException e) {//邮箱无效
            return ResponseUtils.toJson(10, "邮箱无效", null);
        } catch (UserServiceImp.UserExistException e) {//该用户已存在
            return ResponseUtils.toJson(20, "该用户已注册", null);
        } catch (UserServiceImp.EmailExistException e) {
            return ResponseUtils.toJson(21, "该邮箱已注册", null);
        }
        return ResponseUtils.toJson(200, "注册成功", null);
    }

    /**
     * 登录接口
     * @param session
     * @param username 用户名
     * @param password 密码
     * @return
     */
    @RequestMapping(value = "login",method = RequestMethod.POST)
    public Map login(HttpSession session,
                     @RequestParam("username") String username,
                     @RequestParam("password") String password){
        try {
            userService.login(session,username,password);

        } catch (UserServiceImp.UserNotExistException e) {//用户不存在
            return ResponseUtils.toJson(10, "用户不存在", null);
        } catch (UserServiceImp.PasswordErrorException e) { //密码错误
            return ResponseUtils.toJson(11, "密码错误", null);
        } catch (UserServiceImp.UsernameEmptyException e) {//用户名为空
            return ResponseUtils.toJson(0, "用户名为空", null);
        } catch (UserServiceImp.PasswordEmptyException e) {//密码为空
            return ResponseUtils.toJson(1, "密码为空", null);
        }
        return ResponseUtils.toJson(200, "登录成功", null);
    }
}
